/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.Font;
import java.security.Key;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import nu.zoom.swing.desktop.plugin.portunis.Utils;
import nu.zoom.swing.layout.VerticalPanel;

import org.ops4j.gaderian.Messages;

/**
 * A factory for components that dispalys information about a key entry in a
 * keystore.
 * 
 * @author $Author: johan $
 * @version $Revision: 1.1 $
 */
public class KeyComponentFactory
{
	private Messages messages;

	/**
	 * 
	 */
	public KeyComponentFactory(Messages messages) {
		super();
		this.messages = messages;
	}

	/**
	 * Construct a component that shows information about a key.
	 * 
	 * @param key
	 *            The key. Must not be null
	 * @return A component that displays key information.
	 */
	JComponent getKeyDescription(Key key)
	{
		if (key == null) {
			throw new IllegalArgumentException("Key can not be null");
		}
		VerticalPanel component = new VerticalPanel();

		String algorithm = (key.getAlgorithm() != null) ? key.getAlgorithm()
				: messages.getMessage("keystore.general.unknown");
		String format = (key.getFormat() != null) ? key.getFormat() : messages
				.getMessage("keystore.general.unknown");
		String encoded = Utils.getHexString(key.getEncoded(), 80);

		component.addRow(messages.getMessage("keystore.key.algorithm"),
				algorithm);
		component.addRow(messages.getMessage("keystore.key.encoding"), format);

		JLabel encodedLabel = new JLabel(messages
				.getMessage("keystore.key.encoded"));
		Font monospacedFont = Font.decode("Monospaced");
		JTextArea encodedComponent = new JTextArea(encoded);
		encodedComponent.setFont(monospacedFont);
		encodedComponent.setEditable(false);
		component.addRow(encodedLabel, encodedComponent);

		return component;
	}

}
